package net.lab1024.sa.admin.module.business.erp.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.dao.MachineDao;
import net.lab1024.sa.admin.module.business.erp.domain.entity.MachineEntity;
import net.lab1024.sa.admin.module.business.erp.domain.form.add.MachineAddForm;
import net.lab1024.sa.admin.module.business.erp.domain.form.query.MachineQueryForm;
import net.lab1024.sa.admin.module.business.erp.domain.form.update.MachineUpdateForm;
import net.lab1024.sa.admin.module.business.erp.domain.vo.MachineVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Semaphore;

/**
 * 设备表 Service
 *
 * @Author akita
 * @Date 2025-04-17 16:47:56
 * @Copyright akita
 */

@Service
public class MachineService {

    @Resource
    private MachineDao machineDao;


    Semaphore semaphore = new Semaphore(2);


    /**
     * 分页查询
     */
    public PageResult<MachineVO> queryPage(MachineQueryForm queryForm) {


        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<MachineVO> list = machineDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<Long> add(MachineAddForm addForm) {
        MachineEntity machineEntity = SmartBeanUtil.copy(addForm, MachineEntity.class);
        machineDao.insert(machineEntity);
        return ResponseDTO.ok(machineEntity.getDocEntry());
    }

    /**
     * 更新
     *
     */
    public ResponseDTO<String> update(MachineUpdateForm updateForm) {
        MachineEntity machineEntity = SmartBeanUtil.copy(updateForm, MachineEntity.class);
        if (machineDao.updateById(machineEntity) == 0) {
            throw new RuntimeException("数据已被更新，刷新后重试！");
        }

        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        machineDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long docEntry) {


        machineDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }

    public ResponseDTO<MachineEntity> info(Long docEntry) {
        return   ResponseDTO.ok(machineDao.selectById(docEntry));

    }
}
